package com.tencent.texttoimage.hook;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.tencent.texttoimage.util.ToImage;

import java.lang.reflect.Field;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;

import static com.tencent.texttoimage.hook.Injector.TAG;

public class WeChatHook {

    private static boolean sendByLongClick = true;
    private static boolean sendByClick = false;

    private static Field buttonField;

    public static void doHook(ClassLoader classLoader, Context context) throws IllegalAccessException {
        Log.d(TAG, "doHook method invoked!");

        final Class<?> chatFooterClass = XposedHelpers.findClass("com.tencent.mm.pluginsdk.ui.chat.ChatFooter", classLoader);
        Class<?> mMEditTextClass = XposedHelpers.findClass("com.tencent.mm.ui.widget.MMEditText", classLoader);

        final Field mMEditTextField = XposedHelpers.findFirstFieldByExactType(chatFooterClass, mMEditTextClass);

        //  public ChatFooter(Context context, AttributeSet attributeSet, int i) {
        XposedHelpers.findAndHookConstructor(chatFooterClass, Context.class, AttributeSet.class, int.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                if (buttonField == null) {
                    for (Field declaredField : chatFooterClass.getDeclaredFields()) {
                        if (declaredField.getType() == Button.class) {
                            declaredField.setAccessible(true);
                            Button button = (Button) declaredField.get(param.thisObject);
                            if (button != null && "发送".equals(button.getText())) {
                                buttonField = declaredField;
                                break;
                            }
                        }
                    }
                }

                if (buttonField == null) {
                    XposedBridge.log(TAG + ": cannot found send button");
                    return;
                }

                final Object chatFooterObj = param.thisObject;
                Button button = (Button) buttonField.get(chatFooterObj);
                if (sendByClick) {
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                EditText editText = (EditText) mMEditTextField.get(chatFooterObj);
                                generateAndSend(editText);
                            } catch (IllegalAccessException e) {
                                Log.e(TAG, "errors on proxy OnClickListener", e);
                            }
                        }
                    });
                }

                if (sendByLongClick) {
                    button.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            try {
                                EditText editText = (EditText) mMEditTextField.get(chatFooterObj);
                                generateAndSend(editText);
                            } catch (IllegalAccessException e) {
                                Log.e(TAG, "errors on proxy OnLongClickListener", e);
                            }
                            return true;
                        }
                    });
                }

            }
        });

        XposedHelpers.findAndHookMethod(Dialog.class, "show", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                for (StackTraceElement stackTraceElement : new Throwable().getStackTrace()) {
                    if (stackTraceElement.getClassName().contains(WeChatHook.class.getName()) && stackTraceElement.getMethodName().equals("run")) {
                        Class dialogClass = (Class) param.thisObject.getClass();
                        for (Field declaredField : dialogClass.getDeclaredFields()) {
                            if (Button.class.isAssignableFrom(declaredField.getType())) {
                                declaredField.setAccessible(true);
                                Button button = (Button) declaredField.get(param.thisObject);
                                if (button != null) {
                                    String text = String.valueOf(button.getText());
                                    if ("确定".equalsIgnoreCase(text) || "OK".equalsIgnoreCase(text)) {
                                        button.performClick();
                                        ((Dialog) param.thisObject).dismiss();
                                        return;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        });

    }

    private static void generateAndSend(final EditText editText) {// this editText object is Instance of XEditTextEx
        Log.d(TAG, "generateAndSend method invkoed!!!!");


        editText.post(new Runnable() {
            @Override
            public void run() {
                //声明 一个 转图工具类
                String path = ToImage.convertTextToImageAndSave(editText.getContext(),editText.getText().toString());

                editText.setText(path);

            }
        });

    }
}
